<template>
  <div class="popular-view">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="container">
        <h1>🔥 热门美食</h1>
        <p>为您精选最受欢迎的美味佳肴</p>
      </div>
    </div>
    
    <div class="container">
      <div v-if="loading" class="loading-container">
        <p>正在加载热门推荐...</p>
      </div>
      <div v-else class="popular-grid grid grid-3">
        <div v-for="item in popularItems" :key="item.id" class="food-card">
          <div class="card-image">
            <img :src="item.image" :alt="item.name" class="food-image">
            <div class="card-overlay">
              <el-button type="primary" @click="goToDetail(item.id)" class="view-btn">
                查看详情
              </el-button>
            </div>
          </div>
          <div class="food-info">
            <h3>{{ item.name }}</h3>
            <div class="rating-section">
              <div class="rating-stars">
                <span v-for="i in Math.floor(item.rating)" :key="'full-' + i" class="star full">⭐</span>
                <span v-if="item.rating % 1 !== 0" class="star half">⭐</span>
                <span v-for="i in (5 - Math.ceil(item.rating))" :key="'empty-' + i" class="star empty">☆</span>
              </div>
              <div class="rating-info">
                <span class="rating-score">{{ item.rating }}/5</span>
                <span class="review-count">({{ item.reviewCount }}条评价)</span>
              </div>
            </div>
            <p class="description">{{ item.description }}</p>
            <div class="merchant-info">
              <div class="merchant-details">
                <span class="merchant-name">{{ item.merchant }}</span>
                <span class="category-tag">{{ item.category }}</span>
              </div>
              <div class="food-price">¥{{ item.price }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PopularView',
  data() {
    return {
      popularItems: [],
      loading: false
    }
  },
  async mounted() {
    await this.loadPopularFoods()
  },
  methods: {
    async loadPopularFoods() {
      this.loading = true
      try {
        const response = await fetch('http://localhost:2077/api/public/popular-foods?limit=6')
        if (response.ok) {
          const data = await response.json()
          if (data.success) {
            this.popularItems = data.data
          } else {
            console.error('获取热门推荐失败:', data.message)
          }
        }
      } catch (error) {
        console.error('获取热门推荐失败:', error)
      } finally {
        this.loading = false
      }
    },
    goToDetail(id) {
      this.$router.push(`/food/${id}`)
    }
  }
}
</script>

<style scoped>
.popular-view {
  min-height: 100vh;
}

.popular-grid {
  padding: var(--spacing-lg) 0;
}

.food-card {
  position: relative;
  background: var(--background-primary);
  border-radius: var(--radius-xl);
  overflow: hidden;
  box-shadow: var(--shadow-md);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid var(--border-light);
}

.food-card:hover {
  transform: translateY(-12px) scale(1.02);
  box-shadow: var(--shadow-xl);
}

.card-image {
  position: relative;
  overflow: hidden;
}

.food-image {
  width: 100%;
  height: 220px;
  object-fit: cover;
  transition: transform 0.4s ease;
}

.food-card:hover .food-image {
  transform: scale(1.1);
}

.card-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.food-card:hover .card-overlay {
  opacity: 1;
}

.view-btn {
  background: var(--gradient-accent) !important;
  border: none !important;
  padding: 12px 24px !important;
  border-radius: var(--radius-xl) !important;
  font-weight: 600;
  transform: translateY(20px);
  transition: transform 0.3s ease;
}

.food-card:hover .view-btn {
  transform: translateY(0);
}

.food-info {
  padding: var(--spacing-lg);
  text-align: center;
}

.food-info h3 {
  margin: 0 0 var(--spacing-sm) 0;
  color: var(--text-primary);
  font-size: 1.25rem;
  font-weight: 600;
}

.description {
  color: var(--text-secondary);
  margin-bottom: var(--spacing-md);
  line-height: 1.5;
  font-size: 0.95rem;
}

.rating-section {
  margin-bottom: var(--spacing-md);
  text-align: center;
}

.rating-stars {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 2px;
  margin-bottom: var(--spacing-xs);
}

.star {
  font-size: 1.2rem;
  transition: all 0.3s ease;
}

.star.full {
  color: #ffd700;
  text-shadow: 0 0 3px rgba(255, 215, 0, 0.5);
}

.star.half {
  color: #ffd700;
  opacity: 0.7;
  text-shadow: 0 0 3px rgba(255, 215, 0, 0.3);
}

.star.empty {
  color: #ddd;
}

.rating-info {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: var(--spacing-xs);
}

.rating-score {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--secondary-color);
}

.review-count {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

.merchant-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: var(--spacing-md);
  padding-top: var(--spacing-sm);
  border-top: 1px solid var(--border-light);
}

.merchant-details {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-xs);
}

.merchant-name {
  color: var(--text-secondary);
  font-size: 0.9rem;
  font-weight: 500;
}

.category-tag {
  background: var(--gradient-secondary);
  color: white;
  padding: 4px 8px;
  border-radius: var(--radius-md);
  font-size: 0.8rem;
  font-weight: 500;
  align-self: flex-start;
}

.food-price {
  color: var(--primary-color);
  font-size: 1.2rem;
  font-weight: 700;
}

/* 添加加载状态动画 */
@keyframes shimmer {
  0% {
    background-position: -200px 0;
  }
  100% {
    background-position: calc(200px + 100%) 0;
  }
}

.loading-card {
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200px 100%;
  animation: shimmer 1.5s infinite;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .food-card {
    margin-bottom: var(--spacing-md);
  }
  
  .food-image {
    height: 180px;
  }
  
  .food-info {
    padding: var(--spacing-md);
  }
}
</style> 